package principal;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileFilter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import logica.ModeloEjecucion;
import reflection.JarUtils;

public class VentanaPrincipalPanel extends JPanel {
    
    private static final long serialVersionUID = 2539463075277533454L;
    
    public VentanaPrincipalPanel() {
        ImageIcon imgTitulo = new ImageIcon(
                VentanaPrincipalPanel.class.getResource("/imagenes/fondo.jpg"));
        JLabel labelTitulo = new JLabel(imgTitulo, SwingConstants.CENTER);
        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setBackground(null);
        layeredPane.setLayout(null);
        layeredPane.setOpaque(true);
        labelTitulo.setBounds(0, 0, 600, 500);
        labelTitulo.setOpaque(false);
        
        File f = new File("Modulos");
        File[] aFiles = f.listFiles(new FileFilter() {
            
            @Override
            public boolean accept(File pathname) {
                return pathname.isDirectory() && !pathname.isHidden();
            }
        });
        JPanel panelBotones = new JPanel();
        JScrollPane jsp = new JScrollPane(panelBotones);
        panelBotones.setLayout(new GridLayout(aFiles.length, 1, 5, 5));
        panelBotones.setOpaque(false);
        jsp.setBounds(250, 100, 220, 300);
        jsp.setOpaque(false);
        jsp.setBackground(null);
        if (aFiles != null) {
            jsp.setBounds(190, 100 > 200 - (50 * (aFiles.length / 2)) ? 100 : 200 - (50 * (aFiles.length / 2)), 220, 50 * aFiles.length > 350 ? 350 : 50 * aFiles.length);
            for (int i = 0; i < aFiles.length; i++) {
                JButton boton = new JButton(aFiles[i].getName());
                boton.addActionListener(new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        final JButton source = (JButton) e.getSource();
                        Thread t = new Thread(new Runnable() {
                            
                            @Override
                            public void run() {
                                String path = ModeloEjecucion.getPathAProyecto();
                                try {
                                    JFrame frame = (JFrame) JarUtils.getObjectFromJar((path + "/Modulos/" + source.getText() + "/" + source.getText() + "GUI.jar"), "main");
                                    frame.setVisible(true);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    JOptionPane.showMessageDialog(VentanaPrincipalPanel.this.getParent(), "Ha ocurrido un error al cargar el juego. Comprueba que todo sea correcto y vuelve a intentarlo", "ERROR", JOptionPane.ERROR_MESSAGE);
                                    System.exit(1);
                                }
                            }
                        });
                        t.start();
                        Component c = VentanaPrincipalPanel.this.getParent();
                        while (c != null) {
                            c.setVisible(false);
                            if (c instanceof JFrame) {
                                ((JFrame) c).dispose();
                            }
                            c = c.getParent();
                        }
                    }
                });
                panelBotones.add(boton);
            }
        }
        
        layeredPane.add(jsp, 0);
        JLabel tit = new JLabel("Cargador de juegos");
        tit.setHorizontalAlignment(SwingConstants.CENTER);
        tit.setFont(new Font("Arial", Font.BOLD, 40));
        tit.setBounds(50, 25, 500, 50);
        layeredPane.add(tit, 0);
        layeredPane.add(labelTitulo, -1);
        setLayout(null);
        layeredPane.setBounds(0, 0, 600, 500);
        add(layeredPane);
    }
}
